package com.bittech.client.dao;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.bittech.client.entity.Account;
import com.bittech.util.db_utils.BasedDao;
import org.apache.commons.codec.cli.Digest;
import org.apache.commons.codec.digest.DigestUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author: yuisama
 * @Date: 2019-06-04 17:29
 * @Description:用户账户Dao
 */
public class AccountDao extends BasedDao {
    /**
     * 客户端登录
     * @param userName
     * @param password
     * @return
     */
    public Account login(String userName,String password) {
        DruidPooledConnection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Account account = null;
        try {
            connection = getConnection(true);
            String sql =
                    "select * from account where username = ? and password = ?";
            preparedStatement =
                    connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            preparedStatement.setString(2,DigestUtils.md5Hex(password));
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                account = extractAccount(resultSet);

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeResources(preparedStatement,resultSet,connection);
        }
        return account;
    }

    /**
     * 用户注册
     * @param account
     * @return
     */
    public boolean register(Account account) {
        DruidPooledConnection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        boolean isSuccess = false;
        try {
            connection = getConnection(true);
            String sql =
                    "insert into account(username, password, age, brief) values (?,?,?,?)";
            statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            statement.setString(1,account.getUserName());
            statement.setString(2, DigestUtils.md5Hex(account.getPassword()));
            statement.setShort(3,account.getAge());
            statement.setString(4,account.getBrief());
            isSuccess = (statement.executeUpdate() == 1);
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResources(statement,resultSet,connection);
        }
        return isSuccess;
    }

    private Account extractAccount(ResultSet resultSet) throws SQLException {
        Account account = new Account();
        account.setId(resultSet.getInt("id"));
        account.setAge(resultSet.getShort("age"));
        account.setUserName(resultSet.getString("username"));
        account.setUserName(resultSet.getString("password"));
        account.setBrief(resultSet.getString("brief"));
        return account;
    }

}
